package com.meitianhui.convenitenbuy.module.wallet.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseArrayResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletScanCardContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.model.FamilyCard;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletScanCardActivity;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletScanCardPresenter implements WalletScanCardContract.Presenter {

    private WalletScanCardActivity context;
    private final WalletScanCardContract.View view;
    private Gson gson;

    public WalletScanCardPresenter(WalletScanCardActivity view,
                                   WalletScanCardActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void verifyCard(String result) {
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        String userToken = token.getUser_token();
        String service = "finance.consumerPrepayCardBind";

        Map<String, String> map = new HashMap<>();
        map.put("card_no", result);
        map.put("member_id", token.getMember_id());
        map.put("mobile", InfoProvider.getUserInfo(App.getInstance()).getMobile());
        map.put("remark", result);
        String jsonParams = gson.toJson(map);

        WalletApiDataSource.getInstance()
                .getFamilyCards(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseArrayResponse<FamilyCard>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseArrayResponse<FamilyCard> response) {
                        view.verifyCardBack(true, result);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.verifyCardBack(false, msg);
                    }
                });
    }

}
